const workRecords = require("../models/workRecords")
const point = require("../models/point")
const question = require("../models/question")

var express = require('express');
var router = express.Router();
//  知识点标题  题目信息  学生答案  正确答案 解析  题型
const formidable = require('formidable');

router.post('/', (req, res, next) => {
  var form = new formidable.IncomingForm();
  form.parse(req, function (err, fields, files) {
    if (err) {
        res.json({ 'state': false })
    } else {
      workRecords.find({
        seq: fields.seq,
        UUID: fields.UUID
      }, async(err, doc) => {
        if (err) {
          res.json({ 'state': false, 'msg': err });
        } else{
          console.log(doc)

          async function find_ques() {
            let message = [];
            for(let i=0; i<doc[0].question_answer.length; i++) {
              let point_message = await point.find({
                _id: doc[0].question_answer[i].pointId
              })
              message[i] = {}
              message[i].stu_answer = doc[0].question_answer[i].stu_answer
              message[i].pointTitle = point_message[0].title
              if(doc[0].question_answer[i].questionId !== "") {
                //提高题
                let ques_message = await question.find({
                  _id: doc[0].question_answer[i].questionId
                })
                message[i].ques = ques_message[0]
              } else {
                //基础题
                message[i].ques = point_message[0].practice
                //统一两边key
                message[i].ques.question = point_message[0].practice.question_editor_Content
              }
              message[i].pointId = doc[0].question_answer[i].pointId
              message[i].questionId = doc[0].question_answer[i].questionId
            }
            console.log(message)
            return message
          }
          let ques_mes = await find_ques()
          let result_data = []
          for(let i=0; i<ques_mes.length; i++){
            result_data[i] = {}
            result_data[i].message = ques_mes[i]
            result_data[i].ques = ques_mes[i].ques.question
            result_data[i].pointTltle = ques_mes[i].pointTitle
            result_data[i].answer = ques_mes[i].ques.answer
            result_data[i].illustrationContent = ques_mes[i].ques.illustrationContent
            if(ques_mes[i].ques.type==="xuanze"){
              result_data[i].type = "选择"
              let list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
              ques_mes[i].ques.items.forEach((ele,k) => {
                result_data[i].ques = `${result_data[i].ques}<div>${list[k]} ${ele}</div>` 
              })
              result_data[i].stu_answer = ""
              ques_mes[i].stu_answer.forEach(ele => {
                result_data[i].stu_answer = `${result_data[i].stu_answer}<sapn>${list[parseInt(ele)-1]} </span> ` 
              })
              result_data[i].answer = ""
              ques_mes[i].ques.answer.forEach(ele => {
                result_data[i].answer = `${result_data[i].answer}<sapn>${list[parseInt(ele)-1]} </span> ` 
              })
            } else if(ques_mes[i].ques.type==="panduan"){
              result_data[i].type = "判断"
              if(ques_mes[i].stu_answer[0]==="yes"){
                result_data[i].stu_answer = "正确"
              } 
              if(ques_mes[i].stu_answer[0]==="no"){
                result_data[i].stu_answer = "错误"
              }
              if(ques_mes[i].ques.answer[0]==="yes"){
                result_data[i].answer = "正确"
              }
              if(ques_mes[i].ques.answer[0]==="no"){
                result_data[i].answer = "错误"
              }
              if(ques_mes[i].ques.answer === "no"){
                result_data[i].answer = "错误"
              }
              if(ques_mes[i].ques.answer === "yes"){
                result_data[i].answer = "正确"
              }
            } else if(ques_mes[i].ques.type==="jianda"){
              result_data[i].type = "简答"
              result_data[i].stu_answer = ques_mes[i].stu_answer[0]
              result_data[i].answer = "言之有理即可"
              result_data[i].illustrationContent = "言之有理即可"
            }
          }
    
          res.json({ 'state': true, 'data': result_data });
        }
      })
    }
  })
})

module.exports = router;